import { createReducer } from 'reduxsauce'
import { REHYDRATE } from 'redux-persist/es/constants'
import Taro from '@tarojs/taro'
import {
  GET_MY_MEDITATION_SUMMARY,
  GET_SHARABLE_MEDITATION,
  SHARE_MEDITATION,
  SET_DURATION_TIME,
  GET_MEDITATION_DETAIL,
  RESET_SHARE,
  PLAY_MEDITATION_AUDIO,
  STOP_MEDITATION_AUDIO,
  RESET_OUTER_PARAMS,
  SET_OUTER_TOTAL_TIME,
  GET_MEDITATION_INTRO,
  GET_MEDITATION_LIST,
  GET_TAG_MEDITATIONS,
  SET_CURRENT_TIME,
} from '@ACTIONS/meditation'
import { TimeRange } from '@CONSTANTS/meditation'
import { LOGOUT_USER } from '@ACTIONS/auth'

const value = Taro.getStorageSync('durationValue')

const INITIAL_STATE = {
  totalTime: 0,
  todayTotalTime: 0,
  todayMeditationTitle: null,
  durationTime: TimeRange[value] * 60 || 60,
  share: false,
  meditationId: '',
  currentMeditation: null,
  loadingIntro: true,
  meditationIntro: [],
  meditationList: [],
  meditationTagList: [],
  currentTime: 0,
  // 外部播放属性
  outerTotalTime: 0,
  outerPlayTime: 0,
  startTime: undefined,
  stopTime: undefined,
  finished: true
}

const rehydrate = (state, { payload }) => {
  if (payload && payload.meditation) {
    return { ...INITIAL_STATE, ...payload.meditation }
  }
  return {
    ...state
  }
}

export default createReducer(INITIAL_STATE, {
  [REHYDRATE]: rehydrate,

  [GET_MY_MEDITATION_SUMMARY.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [GET_MY_MEDITATION_SUMMARY.SUCCESS]: (state, { data }) => ({
    ...state,
    totalTime: data.TotalTime,
    todayMeditationTitle: data.LastViewedMeditationTitle,
    todayTotalTime: data.TodayTotalTime,
  }),

  [PLAY_MEDITATION_AUDIO.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [PLAY_MEDITATION_AUDIO.SUCCESS]: state => ({
    ...state,
    finished: false,
    startTime: Date.now(),
    stopTime: Date.now(),
  }),

  [STOP_MEDITATION_AUDIO.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [STOP_MEDITATION_AUDIO.SUCCESS]: (state, { finished }) => ({
    ...state,
    startTime: Date.now(),
    stopTime: Date.now(),
    finished: true,
    outerPlayTime: finished ? 0 : state.outerPlayTime + Math.round((Date.now() - state.startTime) / 1000)
  }),

  [GET_SHARABLE_MEDITATION.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [GET_SHARABLE_MEDITATION.SUCCESS]: (state, { data, reset }) => ({
    ...state,
    durationTime: data.Duration || state.durationTime,
    meditationId: data.MeditationId || state.meditationId,
    share: reset ? false : data.Share || false,
  }),

  [SHARE_MEDITATION.FAILURE]: (state, { error }) => ({
    ...state,
    error,
  }),
  [SHARE_MEDITATION.SUCCESS]: (state, { data, share }) => ({
    ...state,
    share,
    totalTime: data.TotalTime,
  }),

  [RESET_SHARE.REQUEST]: state => ({
    ...state,
    share: false,
  }),
  [GET_MEDITATION_INTRO.FAILURE]: (state, { error }) => ({
    ...state,
    loadingIntro: false,
    error
  }),
  [GET_MEDITATION_INTRO.SUCCESS]: (state, { data }) => ({
    ...state,
    meditationIntro: data,
    loadingIntro: false,
  }),
  [GET_MEDITATION_INTRO.REQUEST]: state => ({
    ...state,
    loadingIntro: true,
  }),
  [GET_MEDITATION_LIST.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [GET_MEDITATION_LIST.SUCCESS]: (state, { data }) => ({
    ...state,
    meditationList: data,
  }),
  [GET_TAG_MEDITATIONS.SUCCESS]: (state, { data }) => ({
    ...state,
    meditationTagList: data,
  }),

  [GET_MEDITATION_DETAIL.FAILURE]: (state, { error }) => ({
    ...state,
    error
  }),
  [GET_MEDITATION_DETAIL.SUCCESS]: (state, { data, meditationId }) => ({
    ...state,
    meditationId,
    currentMeditation: data,
  }),
  [GET_MEDITATION_DETAIL.REQUEST]: state => ({
    ...state,
    currentMeditation: null,
  }),

  [SET_DURATION_TIME.REQUEST]: (state, { durationTime }) => ({
    ...state,
    durationTime,
  }),

  [SET_OUTER_TOTAL_TIME.REQUEST]: (state, { totalTime }) => ({
    ...state,
    outerTotalTime: totalTime,
    outerPlayTime: 0,
    startTime: Date.now(),
    stopTime: Date.now(),
  }),
  [SET_CURRENT_TIME.REQUEST]: (state, { currentTime }) => ({
    ...state,
    currentTime,
  }),

  [RESET_OUTER_PARAMS.REQUEST]: state => ({
    ...state,
    startTime: undefined,
    stopTime: undefined,
    outerRealPlayTime: 0,
    outerTotalTime: 0,
    currentTime: 0,
  }),
  [LOGOUT_USER.SUCCESS]: state => ({ ...state, todayTotalTime: 0, todayMeditationTitle: null }),
})
